home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH6 / SRC / BOWDITCH.FRM < prev    next >
Text File  |  1996-03-28  |  5KB  |  199 lines

  1. VERSION 4.00
  2. Begin VB.Form BowditchForm 
  3.    Caption         =   "Bowditch"
  4.    ClientHeight    =   5310
  5.    ClientLeft      =   2085
  6.    ClientTop       =   900
  7.    ClientWidth     =   4830
  8.    Height          =   6000
  9.    Left            =   2025
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   354
  12.    ScaleMode       =   3  'Pixel
  13.    ScaleWidth      =   322
  14.    Top             =   270
  15.    Width           =   4950
  16.    Begin VB.TextBox PText 
  17.       Height          =   285
  18.       Left            =   1320
  19.       TabIndex        =   5
  20.       Text            =   "4"
  21.       Top             =   45
  22.       Width           =   615
  23.    End
  24.    Begin VB.TextBox QText 
  25.       Height          =   285
  26.       Left            =   2400
  27.       TabIndex        =   4
  28.       Text            =   "5"
  29.       Top             =   45
  30.       Width           =   615
  31.    End
  32.    Begin VB.TextBox DtText 
  33.       Height          =   285
  34.       Left            =   240
  35.       TabIndex        =   3
  36.       Text            =   "0.025"
  37.       Top             =   45
  38.       Width           =   615
  39.    End
  40.    Begin VB.CommandButton CmdGo 
  41.       Caption         =   "Go"
  42.       Default         =   -1  'True
  43.       Height          =   375
  44.       Left            =   4200
  45.       TabIndex        =   1
  46.       Top             =   0
  47.       Width           =   615
  48.    End
  49.    Begin VB.PictureBox Canvas 
  50.       AutoRedraw      =   -1  'True
  51.       Height          =   4815
  52.       Left            =   0
  53.       ScaleHeight     =   -2.2
  54.       ScaleLeft       =   -1.1
  55.       ScaleMode       =   0  'User
  56.       ScaleTop        =   1.1
  57.       ScaleWidth      =   2.2
  58.       TabIndex        =   0
  59.       Top             =   480
  60.       Width           =   4815
  61.    End
  62.    Begin VB.Label Label1 
  63.       Caption         =   "P"
  64.       Height          =   255
  65.       Index           =   3
  66.       Left            =   1200
  67.       TabIndex        =   7
  68.       Top             =   60
  69.       Width           =   255
  70.    End
  71.    Begin VB.Label Label1 
  72.       Caption         =   "Q"
  73.       Height          =   255
  74.       Index           =   2
  75.       Left            =   2235
  76.       TabIndex        =   6
  77.       Top             =   60
  78.       Width           =   255
  79.    End
  80.    Begin VB.Label Label1 
  81.       Caption         =   "dt"
  82.       Height          =   255
  83.       Index           =   1
  84.       Left            =   0
  85.       TabIndex        =   2
  86.       Top             =   60
  87.       Width           =   255
  88.    End
  89.    Begin VB.Menu mnuFile 
  90.       Caption         =   "&File"
  91.       Begin VB.Menu mnuFileExit 
  92.          Caption         =   "E&xit"
  93.       End
  94.    End
  95. End
  96. Attribute VB_Name = "BowditchForm"
  97. Attribute VB_Creatable = False
  98. Attribute VB_Exposed = False
  99. Option Explicit
  100.  
  101. Const PI = 3.14159
  102. Const TWO_PI = 2 * PI
  103.  
  104. Dim P As Integer
  105. Dim Q As Integer
  106.  
  107. ' ************************************************
  108. ' Draw the curve on the indicated picture box.
  109. ' ************************************************
  110. Sub DrawCurve(pic As PictureBox, start_t As Single, stop_t As Single, Dt As Single)
  111. Dim t As Single
  112.  
  113.     pic.Cls
  114.     pic.CurrentX = X(start_t)
  115.     pic.CurrentY = Y(start_t)
  116.     
  117.     t = start_t + Dt
  118.     Do While t < stop_t
  119.         pic.Line -(X(t), Y(t))
  120.         t = t + Dt
  121.     Loop
  122.     
  123.     pic.Line -(X(stop_t), Y(stop_t))
  124. End Sub
  125.  
  126.  
  127.  
  128. ' ************************************************
  129. ' Non-recursively compute the greatest common
  130. ' divisor of to integers.
  131. ' ************************************************
  132. Private Function GCD(ByVal a As Integer, ByVal b As Integer) As Integer
  133. Dim B_Mod_A As Integer
  134.  
  135.     B_Mod_A = b Mod a
  136.     Do While B_Mod_A <> 0
  137.         ' Prepare the arguments for the "recursion."
  138.         b = a
  139.         a = B_Mod_A
  140.         B_Mod_A = b Mod a
  141.     Loop
  142.  
  143.     GCD = a
  144. End Function
  145.  
  146. ' ************************************************
  147. ' Calculate the values t must cross to draw a
  148. ' Bowditch Curve.
  149. ' ************************************************
  150. Sub SetTBounds(tmin As Single, tmax As Single)
  151.     tmin = 0
  152.     tmax = LCM(P, Q) / P / Q * TWO_PI
  153.     If P Mod 2 = 1 And Q Mod 2 = 1 Then
  154.         tmin = -tmax / 4
  155.         tmax = tmax / 4
  156.     End If
  157. End Sub
  158.  
  159. ' ************************************************
  160. ' Find the least common multiple of two integers.
  161. ' ************************************************
  162. Function LCM(a As Integer, b As Integer) As Integer
  163.     LCM = a * b / GCD(a, b)
  164. End Function
  165.  
  166. ' ************************************************
  167. ' The parametric function Y(t).
  168. ' ************************************************
  169. Function Y(t As Single) As Single
  170.     Y = Sin(Q * t)
  171. End Function
  172.  
  173. ' ************************************************
  174. ' The parametric function X(t).
  175. ' ************************************************
  176. Function X(t As Single) As Single
  177.     X = Sin(P * t)
  178. End Function
  179.  
  180. Private Sub CmdGo_Click()
  181. Dim tmin As Single
  182. Dim tmax As Single
  183. Dim Dt As Single
  184.  
  185.     P = CInt(PText.Text)
  186.     Q = CInt(QText.Text)
  187.     
  188.     SetTBounds tmin, tmax
  189.     
  190.     Dt = CSng(DtText.Text)
  191.     DrawCurve Canvas, tmin, tmax, Dt
  192. End Sub
  193.  
  194. Private Sub mnuFileExit_Click()
  195.     Unload Me
  196. End Sub
  197.  
  198.  
  199.